<?php

class Towns_IndexController extends App_Controller_Action { 
/**
     * Отображает главную страницу
     */
    public function indexAction() 
    {
    	$modelTowns = new Towns();
    	$towns = $modelTowns->getTowns();
        $this->view->towns = $towns;
    }

    /**
     * Страница из меню
     */    
    public function viewAction() 
    {
        $townId = $this->_getParam('townId');
        $modelTowns = new Towns();
        $town = $modelTowns->getTowns($townId);
        $this->view->town = $town;
    }

    public function adminlistAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$modelTowns = new Towns();
    	$towns = $modelTowns->getTowns();
        $this->view->towns = $towns;
    }
    
    public function adminformAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	if ($this->_request->isPost()) {
    		$formTown = new FormTowns();
    		if ($formTown->isValid($this->_getAllParams())) {
    			$post = $this->_request->getPost();
    			$town = new Towns();
    			$town->setDataFromArray($post);
    			if ($town->id) {
    				$town->update();
	    		} else {
    				$town->insert();
	    		}
	    		$this->_helper->redirector->gotoRoute(array(), 'adminTowns');
    		}
			$this->view->formTowns = $formTown;
    	} else {
	    	$id = $this->_getParam('townId');
	        $modelTowns = new Towns();
	        $town = $modelTowns->getTown($id);
			$formTowns = new FormTowns($town);
			$this->view->formTowns = $formTowns;
    	}
    }

    public function admindeleteAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$id = $this->_getParam('townId');
        $modelTowns = new Towns();
        $res = $modelTowns->delete('id='.$id);
		$this->_helper->redirector->gotoRoute(array(), 'adminTowns');
    }
}